package com.lindroy.androidplayer.ui.login

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.App
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.databinding.FragmentLoginBinding
import com.lindroy.androidplayer.logic.network.base.http.observeState
import com.lindroy.androidplayer.utils.saveLoginInfo
import com.lindroy.androidplayer.viewmodels.LoginViewModel
import com.lindroy.lib_utils.extensions.shortToast

/**
 * @author Lin
 * @date 2022/1/28
 * @function
 */
class LoginFragment : BaseLazyFragment<FragmentLoginBinding>() {

    private val viewModel by viewModels<LoginViewModel>()

    override fun FragmentLoginBinding.initView(savedInstanceState: Bundle?) {
        vm = viewModel
    }

    override fun FragmentLoginBinding.initListener() {
        viewModel.loginLiveData.observeState(viewLifecycleOwner) {
            onSuccess {
                shortToast(R.string.login_success)
                App.eventViewModel.postLoginInfo(it)
                saveLoginInfo(it)
                findNavController().navigateUp()
            }
        }

        viewModel.toRegisterLiveData.observe(viewLifecycleOwner) {
            findNavController().navigate(R.id.action_login_to_register)
        }

        viewModel.loginIllegalMessage.observe(viewLifecycleOwner) {
            shortToast(it)
        }
        registerLoadingDialogListener()
    }

    override fun lazyLoad() {
    }
}